﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace TerminationCall.Core
{
    public class TileSet
    {
        private Bitmap _bitmap;
        private Tile[,] _tiles;
        public string Name { get; set; }
        public string FileName { get; private set; }
        public List<TileObjectGroup> TileObjectGroups { get; set; }

        public TileSet(string path, int horizontalTiles, int verticalTiles) {
            _tiles = new Tile[horizontalTiles, verticalTiles];
            for (int x = 0; x < horizontalTiles; x++) {
                for (int y = 0; y < verticalTiles; y++) {
                    _tiles[x, y] = new Tile();
                    _tiles[x, y].TileSet = this;
                }
            }

            Bitmap = new Bitmap(Image.FromFile(path));

            Name = Path.GetFileNameWithoutExtension(path);
            FileName = path;

            TileObjectGroups = new List<TileObjectGroup>();
        }

        public Bitmap Bitmap
        {
            get
            {
                return _bitmap;
            }
            set
            {

                _bitmap = value;
                
                for (int y = 0; y < 20; y++)
                {
                    for (int x = 0; x < 20; x++)
                    {
                        _tiles[x, y].SetBitmap(this, x, y);
                    }                    
                }
            }
        }

        public Tile[,] Tiles
        {
            get
            {
                return _tiles;
            }
        }

        public void GroupObjectTile(Rectangle rectangle, string name) {

            if (string.IsNullOrEmpty(name)) {
                return;
            }

            if (rectangle.IsEmpty) {
                return;
            }

            TileObjectGroup tileObjectGroup = new TileObjectGroup();

            tileObjectGroup.Name = name;
            tileObjectGroup.Rectangle = rectangle;

            TileObjectGroups.Add(tileObjectGroup);

        }

        public void UngroupObjectTile(int x, int y) {

            foreach (TileObjectGroup tileObjectGroup in TileObjectGroups) {
                if (tileObjectGroup.Rectangle.X == x && tileObjectGroup.Rectangle.Y == y) {
                    TileObjectGroups.Remove(tileObjectGroup);
                    return;
                }

                if (tileObjectGroup.Rectangle.Left < x) {
                    if (tileObjectGroup.Rectangle.Top < y) {
                        if (tileObjectGroup.Rectangle.Right > x) {
                            if (tileObjectGroup.Rectangle.Bottom > y) {
                                TileObjectGroups.Remove(tileObjectGroup);
                                return;
                            }
                        }
                    }
                }
            }

        }

        public void GroupAutoborderTile(TileFields tileFields, Tile tile, Tile parentTile) {

            if (tile == null) {
                return;
            }

            if (parentTile == null) {
                return;
            }

            parentTile.ChildTileField.SetTileFast(tileFields, tile);
            tile.ParentTile = parentTile;
            tile.TileFields = tileFields;
            tile.ParentTile.TileFields = TileFields.MiddleTile;

        }

        public void UngroupAutoborderTile(Tile tile) {

            Tile[] childTiles = tile.ChildTileField.Tiles;

            foreach (Tile childTile in childTiles) {
                UngroupAutoborderTile(childTile);
            }

            if (tile.ParentTile != null) {

                tile.ParentTile.ChildTileField.SetTile(tile.TileFields, null);
                tile.ParentTile = null;
                tile.TileFields = TileFields.MiddleTile;

            }

        }

    }
}
